package com.cmonbaby.adapter;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * 下拉刷新的帮助类
 * @author simon 2016-11-04 下午16:05:29
 * @since JDK1.8 SDK6.0.1
 * @version V2.3.4
 */
public class SwipeRefreshHelper {

    private SwipeRefreshLayout swipeRefresh;

    public static SwipeRefreshHelper createSwipeRefreshHelper(SwipeRefreshLayout swipeRefresh) {
        return new SwipeRefreshHelper(swipeRefresh);
    }

    public static SwipeRefreshHelper createSwipeRefreshHelper(SwipeRefreshLayout swipeRefresh, @ColorRes int... colorResIds) {
        return new SwipeRefreshHelper(swipeRefresh, colorResIds);
    }

    private SwipeRefreshHelper(@Nullable SwipeRefreshLayout swipeRefresh) {
        this.swipeRefresh = swipeRefresh;
        init();
    }

    private SwipeRefreshHelper(@Nullable SwipeRefreshLayout swipeRefresh, @ColorRes int... colorResIds) {
        this.swipeRefresh = swipeRefresh;
        init(colorResIds);
    }

    private void init(@ColorRes int... colorResIds) {
        if (colorResIds == null || colorResIds.length == 0) {
            swipeRefresh.setColorSchemeResources(android.R.color.holo_orange_dark,
                    android.R.color.holo_green_dark, android.R.color.holo_blue_dark);
        } else {
            swipeRefresh.setColorSchemeResources(colorResIds);
        }
        swipeRefresh.setOnRefreshListener(() -> { // 下拉刷新
            if (mOnSwipeRefreshHelperListener != null)
                mOnSwipeRefreshHelperListener.onRefresh();
        });
    }

    public interface OnSwipeRefreshHelperListener {
        void onRefresh();
    }

    private OnSwipeRefreshHelperListener mOnSwipeRefreshHelperListener;

    public void setOnSwipeRefreshHelperListener(OnSwipeRefreshHelperListener onSwipeRefreshHelperListener) {
        this.mOnSwipeRefreshHelperListener = onSwipeRefreshHelperListener;
    }
}